<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class listener {
    private $tu;
    public $r=0;

    function __construct($xml, $ip) {
	$dom = new DomDocument();
	$dom->loadXML($xml);
	$tu = $dom->documentElement;
	$idTU = $tu->getAttribute("id");

	/*
	 * Vérification que le TU existe, que la clé envoyée est la bonne, et que
	 * l'ip depuis lequel c'est envoyé est le bon
	 */
	if (! ($tb_tu = TuPeer::retrieveByPK($idTU))) return;
	if (! ($tb_tu->getCle() == $this->getNodeText("key", $tu))) return;
	if (! ($tb_tu->getIp() == $ip)) return;

	/*
	 * Vérification version
	 */
	$tb_tu->setVersion($this->getNodeText("version", $tu));
	if (! ($v = DateVersionPeer::retrieveByPK($tb_tu->getId(), $tb_tu->getVersion()))) {
	    $v = new DateVersion();
	    $v->setTuId($tb_tu->getId());
	    $v->setVersion($tb_tu->getVersion());
	    $v->save();
	}
	// si le tu a été mis à jour, on enlève la demande de mise à jour
	if (($tb_tu->getVersionDemandee()) && (version_compare( $tb_tu->getVersion(), $tb_tu->getVersionDemandee(), ">="))) {
	    $tb_tu->setIsMajAsked(false);
	    $tb_tu->setVersionDemandee(null);
	}
	/*
	 * Enregistrement des données concernant le TU
	 */
	$tb_tu->setDateTuUpdate(strtotime($this->getNodeText("date", $tu)));
	$tb_tu->setNbPatients($this->getNodeText("nbPatients", $tu));
	$tb_tu->setNbMedecins($this->getNodeText("nbMedecins", $tu));
	$tb_tu->setDateUpdate(time());
	if ($this->getNodeText("idVeille", $tu) != "") $tb_tu->setIdVeille($this->getNodeText("idVeille", $tu));
	// sauvegarde du TU
	$tb_tu->save();
	$this->tu = $tb_tu;
        /*
	 * Enregistrement des données pour les enquêtes
	 */
	$xmlEnquetes = $tu->getElementsByTagName("enquete");
	$tabIdEnq = array();
	foreach ($xmlEnquetes as $xmlEnq) {
	    $tabIdEnq[$xmlEnq->getAttribute("id")] = $xmlEnq;
	}
	$criteria = new Criteria();
	$criteria->add(EnquetePeer::TU_ID, $idTU);
	$enquetes = EnquetePeer::doSelect($criteria);
	foreach($enquetes as $tbEnq) {
	    if (!(array_key_exists($tbEnq->getId(), $tabIdEnq))) $tbEnq->delete();
	    else {
		$tbEnq->setIsActive($this->getNodeText("value", $tabIdEnq[$tbEnq->getId()]));
		if ($tbEnq->getIsActive()) $tbEnq->setIsAsked(false);
		$tbEnq->save();
		unset($tabIdEnq[$tbEnq->getId()]);
	    }
	}
	foreach($tabIdEnq as $key => $value) {
	    $tbEnq = new Enquete();
	    $tbEnq->setTuId($idTU);
	    $tbEnq->setId($key);
	    $tbEnq->setName($this->getNodeText("name", $value));
	    $temp = $this->getNodeText("value", $value);
	    $tbEnq->setIsActive($temp ? true : false);
	    $tbEnq->save();
	}

        /*
	 * Enregistrement des données pour les options
	 */
	$tabXmlOptCat = $tu->getElementsByTagName("options")->item(0)->getElementsByTagName("categ");
	foreach ($tabXmlOptCat as $xmlOptCat) {
	    $criteria = new Criteria();
	    $criteria->add(OptCategPeer::NAME, $xmlOptCat->getAttribute("nom"));
	    if (!($optCat = OptCategPeer::doSelectOne($criteria))) {
		$optCat = new OptCateg();
		$optCat->setName($xmlOptCat->getAttribute("nom"));
		$optCat->save();
	    }
	    $idOptCat = $optCat->getId();
	    $tabIdOpt = array();
	    foreach ($xmlOptCat->getElementsByTagName("option") as $xmlOpt) {
		$tabIdOpt[$xmlOpt->getAttribute("id")] = $xmlOpt;
	    }
	    $criteria = new Criteria();
	    $criteria->add(OptPeer::TU_ID, $idTU, Criteria::EQUAL);
	    $criteria->add(OptPeer::OPT_CATEG_ID, $idOptCat);
	    $options = OptPeer::doSelect($criteria);
	    foreach($options as $tbOpt) {
		if (!(array_key_exists($tbOpt->getId(), $tabIdOpt))) $tbOpt->delete();
		else {
		    $tbOpt->setValue($this->getNodeText("value", $tabIdOpt[$tbOpt->getId()]));
		    if ($tbOpt->getValue() == $tbOpt->getValueAsked()) $tbOpt->setValueAsked(null);
		    $tbOpt->save();
		    unset($tabIdOpt[$tbOpt->getId()]);
		}
	    }
	    foreach($tabIdOpt as $key => $value) {
		$opt = new Opt();
		$opt->setTuId($idTU);
		$opt->setId($key);
		$opt->setOptCategId($idOptCat);
		$opt->setName($this->getNodeText("name", $value));
		$temp = $this->getNodeText("value", $value);
		$opt->setValue($temp);
		$opt->save();
	    }
	    $this->r = 1;
	}

	/*
	 * Enregistrement des données pour les tests du index.test.php
	 */
	$criteria = new Criteria();
	$criteria->add(TestPeer::TU_ID, $tb_tu->getId());
	$a = TestPeer::doSelect($criteria);
	foreach($a as $aa) $aa->delete();


	$tabXmlTestCat = $tu->getElementsByTagName("tests")->item(0)->getElementsByTagName("categ");
	foreach ($tabXmlTestCat as $xmlTestCat) {
	    $criteria = new Criteria();
	    $criteria->add(TestCategPeer::NAME, $xmlTestCat->getAttribute("name"));
	    if (!($testCat = TestCategPeer::doSelectOne($criteria))) {
		$testCat = new TestCateg();
		$testCat->setName($xmlTestCat->getAttribute("name"));
		$testCat->save();
	    }
	    $idTestCat = $testCat->getId();

	    foreach($xmlTestCat->getElementsByTagName("test") as $xmlTest) {
		$test = new Test();
		$test->setTuId($tb_tu->getId());
		$test->setTestCategId($idTestCat);
		$test->setName($this->getNodeText("name", $xmlTest));
		$test->setIsOk($this->getNodeText("value", $xmlTest));
		if ($this->getNodeText("error", $xmlTest) != "")
		    $test->setError($this->getNodeText("error", $xmlTest));
		$test->save();
	    }

	}
    }

    function getNodeText($name, $root) {
	if ($root->getElementsByTagName($name) && ($root->getElementsByTagName($name)->item(0)))
	    return $root->getElementsByTagName($name)->item(0)->nodeValue;
	return "";
    }

    function getTU() { return $this->tu ; }

    function getReturn() { return $this->r;}


}
